//
//  SLUserInfoController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/13.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLUserInfoController.h"
#import "SLUserDetailInfoCell.h"
#import "XMPPvCardTemp.h"

static NSString *kDetailInfoCellId = @"SLUserDetailInfoCell";

@interface SLUserInfoController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIImageView *cellImgView;

@end

@implementation SLUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SLUserDetailInfoCell class]) bundle:nil] forCellReuseIdentifier:kDetailInfoCellId];
//    [self setupTabelFooterView];
}

- (void)setupTabelFooterView {
    
    UIView *tableFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 100)];
    tableFooter.backgroundColor = [UIColor whiteColor];
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 10.0)];
    sectionView.backgroundColor = [UIColor lightGrayColor];
    [tableFooter addSubview:sectionView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(23.0, 10.0, (kWIDTH - 23.0 * 2), 44.0);
    btn.backgroundColor = SLColor(185, 157, 87, 1);
    [btn setTitle:@"退出登录" forState:(UIControlStateNormal)];
    [btn addTarget:sectionView action:@selector(logoutCurrentUser:) forControlEvents:UIControlEventTouchUpInside];
    [tableFooter addSubview:btn];
    self.tableView.tableFooterView = tableFooter;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 3 : 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SLUserDetailInfoCell *detailCell = [tableView dequeueReusableCellWithIdentifier:kDetailInfoCellId];
    detailCell.headerImgView.hidden = !(indexPath.section == 0 && indexPath.row == 0);
    XMPPvCardTemp *myCart = [XMPPManager sharedManager].vCartModule.myvCardTemp;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                [detailCell configCellWithTitle:@"头像" subtitle:@""];
                detailCell.headerImgView.image = [UIImage imageWithData:myCart.photo];
                self.cellImgView = detailCell.headerImgView;
                break;
            case 1:
                [detailCell configCellWithTitle:@"昵称" subtitle:myCart.nickname];
                break;
            case 2:
                [detailCell configCellWithTitle:@"电话" subtitle:[UserLocalInfo sharedUserInfo].userId];
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                [detailCell configCellWithTitle:@"公司" subtitle:myCart.orgName];
                break;
            case 1:{
                NSString *depart = (myCart.orgUnits.count > 0) ? myCart.orgUnits.firstObject : @"";
                [detailCell configCellWithTitle:@"部门" subtitle:depart];
            }
                break;
            case 2:
                [detailCell configCellWithTitle:@"职位" subtitle:myCart.title];
                break;
            case 3:
                [detailCell configCellWithTitle:@"MSN" subtitle:myCart.note];
                break;
            case 4:{
                NSString *emailAdress = @"";
                if (myCart.emailAddresses.count > 0) {
//                    SLog(@"email = %@", myCart.emailAddresses);
                    XMPPvCardTempEmail *email = myCart.emailAddresses.firstObject;
//                    SLog(@"XMPPvCardTempEmail = %@", email);
                    DDXMLElement *element = [email elementForName:@"USERID"];
//                    SLog(@"DDXMLElement = %@", element);
                    emailAdress = [element stringValue];
//                    SLog(@"emailAdress = %@", emailAdress);
                }
                [detailCell configCellWithTitle:@"邮件" subtitle:emailAdress];
            }
                break;
                
            default:
                break;
        }
    }
    return detailCell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                [alert addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self showImagePickerView:UIImagePickerControllerSourceTypeCamera];
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self showImagePickerView:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                }]];
                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 1:{
                
            }
                break;
            case 2:{
            
            }
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:{
                
            }
                break;
            case 1:{
                
            }
                break;
            case 2:{
                
            }
                break;
            case 3:{
                
            }
                break;
            case 4:{
                
            }
                break;
                
            default:
                break;
        }
    }
}

- (void)showImagePickerView:(UIImagePickerControllerSourceType)type {
//    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//        return;
//    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = type;
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


- (void)logoutCurrentUser:(id)sender {
    [[XMPPManager sharedManager] userLogout];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
    self.cellImgView.image = img;
    [picker dismissViewControllerAnimated:YES completion:nil];
    //成功之后要同步到服务器
    XMPPvCardTemp *vCart = [XMPPManager sharedManager].vCartModule.myvCardTemp;
    vCart.photo = UIImageJPEGRepresentation(img, 0.5);
    [[XMPPManager sharedManager].vCartModule updateMyvCardTemp:vCart];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
